<template>
  <el-dialog
    :close-on-click-modal="false"
    :title="!dataForm.id ? '新增' : '修改'"
    :visible.sync="visible"
  >
    <el-form
      ref="dataForm"
      :model="dataForm"
      :rules="dataRule"
      label-width="140px"
      @keyup.enter.native="dataFormSubmit()"
    >
      <el-form-item label="标题" prop="name">
        <el-input v-model="dataForm.name" placeholder="请输入标题" />
      </el-form-item>
      <el-form-item label="图片地址" prop="pic">
        <el-input v-model="dataForm.pic" placeholder="图片地址" />
        <single-upload v-model="dataForm.pic" />
      </el-form-item>
<!--      <el-form-item label="活动有效期">-->
<!--        &lt;!&ndash;        <el-input v-model="dataForm.startTime" placeholder="开始时间" />&ndash;&gt;-->
<!--        <el-date-picker-->
<!--          v-model="chooseDate"-->
<!--          type="daterange"-->
<!--          :picker-options="pickerOptions"-->
<!--          range-separator="至"-->
<!--          start-placeholder="开始日期"-->
<!--          end-placeholder="结束日期"-->
<!--          align="right"-->
<!--        />-->
<!--      </el-form-item>-->
      <!--      <el-form-item label="结束时间" prop="endTime">-->
      <!--        <el-input v-model="dataForm.endTime" placeholder="结束时间" />-->
      <!--      </el-form-item>-->
      <el-form-item label="状态">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="1">启用</el-radio>
          <el-radio :label="0">停用</el-radio>
        </el-radio-group>
      </el-form-item>
      <!--      <el-form-item label="点击数" prop="clickCount">-->
      <!--        <el-input-number v-model="dataForm.clickCount" :min="1" :max="10" label="点击数" />-->
      <!--      </el-form-item>-->
      <el-form-item label="广告详情连接地址" prop="url">
        <el-input v-model="dataForm.url" placeholder="广告详情连接地址" />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <!--        <el-input v-model="dataForm.sort" placeholder="排序" />-->
        <el-input-number v-model="dataForm.sort" :min="1" :max="999" label="排序" />
      </el-form-item>
      <el-form-item label="备注" prop="note">
        <el-input v-model="dataForm.note" type="textarea" :rows="3" placeholder="备注" />
      </el-form-item>
      <!--      <el-form-item label="发布者" prop="publisherId">-->
      <!--        <el-input v-model="dataForm.publisherId" placeholder="发布者" />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="审核者" prop="authId">-->
      <!--        <el-input v-model="dataForm.authId" placeholder="审核者" />-->
      <!--      </el-form-item>-->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addHome_adv, getHome_adv } from '@/api/sms/home_adv'
import SingleUpload from '@/components/Upload/SingleUpload'
const { shortcuts } = require('@/utils')

export default {
  components: { SingleUpload },
  data() {
    return {
      visible: false,
      catePath: [],
      chooseDate: [],
      pickerOptions: shortcuts,
      dataForm: {
        id: 0,
        name: '',
        pic: '',
        startTime: '',
        endTime: '',
        status: 1,
        clickCount: 0,
        url: '',
        note: '',
        sort: 0
      },
      dataRule: {
        name: [
          { required: true, message: '名字不能为空', trigger: 'blur' }
        ],
        pic: [
          { required: true, message: '图片地址不能为空', trigger: 'blur' }
        ],
        startTime: [
          { required: true, message: '开始时间不能为空', trigger: 'blur' }
        ],
        endTime: [
          { required: true, message: '结束时间不能为空', trigger: 'blur' }
        ]
        // url: [
        //   { required: true, message: '广告详情连接地址不能为空', trigger: 'blur' }
        // ]
      }
    }
  },
  // watch: {
  //   catePath(path) {
  //     // 监听到路径变化需要查出这个三级分类的分组信息
  //     console.log('路径变了', path)
  //     this.dataForm.cateId = path[path.length - 1]
  //   }
  // },
  methods: {
    init(id) {
      this.catePath = []
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          getHome_adv(this.dataForm.id).then(({ data }) => {
            this.dataForm = data
            this.catePath = data.catePath
            this.chooseDate = [data.startTime, data.endTime]
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.dataForm.startTime = this.chooseDate[0]
      this.dataForm.endTime = this.chooseDate[1]
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (!this.dataForm.id) {
            addHome_adv(this.dataForm).then(({ data }) => {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          } else {
            addHome_adv(this.dataForm).then(({ data }) => {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          }
        }
      })
    }
  }
}
</script>
